<?php
namespace Home\Controller;
use Org\Util\Tree;

class IndexController extends HomeController{

	//系统首页
    public function index(){
 
        $idarr=M('cate')->where(array('type'=>1,'pid'=>0))->order('id asc')->limit(2)->getField('id',true);
    	$this->assign('idarr',$idarr);
        $this->display();
    }
    public function artc(){

    	$id=I('id');
    	
    	$info=D('Article')->get_info($id);
    	
    	
    	
    	
    	if($info['status']!=1&&$info['uid']!=$_SESSION['zs_home']['user_auth']['uid']){
    		
    		$this->error('你无权查看该内容！','',false,true);
    		
    	}
    	D('Article')->where(array('id'=>$id))->setInc('view',1);	
			
			if(!empty($info['tag'])){
			$tags=explode(',', $info['tag']);
			$info['linktag']='';
			
			foreach($tags as $key1 =>$vo1){
				$maptag['title']=$vo1;
				$maptag['type']=1;
				$tagid=M('tags')->where($maptag)->getField('id');
				
				$url=ZSU('/tagart/'.$tagid,'Index/tagart',array('id'=>$tagid));
				$info['linktag'].='<a style="margin-left:5px;" href="'.$url.'">['.$vo1.']</a>';	
				
				
			}
			}
			
        
		$shareurl='http://'.$_SERVER['HTTP_HOST'].ZSU('/artc/'.$info['id'],'Index/artc',array('id'=>$info['id']));	
		$sharedes='原文链接：'.$shareurl;	
    	$sharetitle=$info['title'].'-'.C('WEB_SITE_TITLE');
    	
    	$authorinfo=query_user(array('space_url','signature','avatar64','nickname'),$info['uid']);
    
    	
    	$this->assign('shareurl',$shareurl);
    	$this->assign('sharedes',$sharedes);
    	$this->assign('sharetitle',$sharetitle);
    	
    	$this->assign('authorinfo',$authorinfo);
    	$this->assign('info',$info);
    	$this->assign('webdescription',$info['title']);
    	$this->assign('webkeyword',$info['title']);
    	$this->assign('webtitle',$info['title']);
          
        $this->display();
    }
    public function search(){
        $keyword=I('keyword','','strip_tags');
       
        $this->assign('keyword',$keyword);  
      
           $this->assign('webdescription',$keyword);
    	$this->assign('webkeyword',$keyword);
    	$this->assign('webtitle',$keyword);   
        $this->display();
    }
public function tagart(){
        $id=I('id',0);
        
        if($id==0){
        	$this->error('请选择一个标签查询！','Index/artlist',false,true,'Public:404');
        	
        }
	    $map['id']=$id;
	    $tag=M('tags')->where($map)->getField('title');
       $this->assign('webdescription',$tag);
    	$this->assign('webkeyword',$tag);
    	$this->assign('webtitle',$tag);
        $this->assign('tag',$tag);    
        $this->display();
    }
    public function artlist(){
    	
    	 $cid=I('cid',0,'int');
    	 
    	if($cid=='all'){
    		//$cid=0;
    	}
        $m = D('cate');
        $catelist = $m->field('*,CONCAT(spid,id) as path2')->order('path2')->select();
        $t = new tree();
        $catelistarr = $t->unlimitCategoryFormat($catelist);
        $catehtml=$t->treeFormat($catelistarr);
      
       
        $this->assign('cid',$cid);  
        $this->assign('cparent',getcidparent($cid));  
        
        $this->assign('webdescription',get_cate_nameByid($cid));
    	$this->assign('webkeyword',get_cate_nameByid($cid));
    	$this->assign('webtitle',get_cate_nameByid($cid));

    	
        $this->assign('catehtml',$catehtml);  
          
          
    	$this->display();
    }
    public function ding(){
    	
    	$uid=is_login();
    	$id=I('get.id');
    	if(cookie($uid.'ding'.$id)!=''||cookie($uid.'cai'.$id)!=''){
    		$this->error('你已经对该内容进行过操作！');
    	}
    	$res=M('Article')->where(array('id'=>$id))->setInc('ding',1);
    	if($res===false){
    		$this->error('操作失败');
    	}else{
    		cookie($uid.'ding'.$id,1);
    		$this->success('操作成功','',array('id'=>'ding'));
    	}
    	
    }
    public function cai(){
    $uid=is_login();
    	$id=I('get.id');
        if(cookie($uid.'ding'.$id)!=''||cookie($uid.'cai'.$id)!=''){
    		$this->error('你已经对该内容进行过操作！');
    	}
    	$res=M('Article')->where(array('id'=>$id))->setInc('cai',1);
    	if($res===false){
    		$this->error('操作失败');
    	}else{
    		cookie($uid.'cai'.$id,1);
    		$this->success('操作成功','',array('id'=>'cai'));
    	}
    	
    
    }
}